//
//  PCDMPApiNavigateTo.m
//  PCDBank
//
//  Created by DK on 2021/2/23.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiNavigateTo.h"
#import "PCDPageViewMange.h"
#import "PCDBankWebViewController.h"
#import "UIView+Toast.h"
#import "PCDDataBaseModel.h"
#import "PCScanViewController.h"

@implementation PCDApiNavigateTo

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback{
    //
    UIViewController *viewPage = context.viewController;
    NSDictionary *tempDic = (NSDictionary *)data;
    NSString *callBack = [tempDic objectForKey:@"callback"];
    NSString *strUrl = [tempDic objectForKey:@"url"];
    
    
    //packageCode bid 离线包编号
    NSString *appId = [tempDic objectForKey:@"appId"];
    
    //应用编号，appId
    NSString *appName = [tempDic objectForKey:@"appName"];
    
    NSString *strFrameworkType = [tempDic objectForKey:@"frameworkType"];
    NSString *extraData =[tempDic objectForKey:@"extraData"];
    [PCDDataBaseModel shareModel].extraData = extraData;
    
    if ([strUrl hasPrefix:@"/"]) {
        strUrl = [strUrl substringFromIndex:1];
    }
    
    if ([PCDUtil nilOrEmptyString:appId]) {
        appId = @"";
    }
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if([PCDPageViewMangeGet().appId isEqual:appId]) {
        NSDictionary *params = @{@"curl":strUrl,@"appId":appId};
        UIViewController *viewController=[PCDPageViewMangeGet() creatCacheWebController:params View:viewPage Status:NO];
        [viewPage.navigationController pushViewController:viewController animated:YES];
        
        NSArray *viewControlles = viewPage.navigationController.viewControllers;
        BOOL ishaveScan = NO;
        for (int i=0; i < [viewControlles count]; i++) {
            UIViewController *vc = [viewControlles objectAtIndex:i];
            if ([vc isKindOfClass:[PCScanViewController class]]) {
                ishaveScan = YES;
            }
        }
        if (ishaveScan) {
            if (viewControlles > 0) {
                BOOL isClean = NO;
                NSMutableArray *newviewControlles = [NSMutableArray array];
                for (int i=0; i < [viewControlles count]; i++) {
                    UIViewController *vc = [viewControlles objectAtIndex:i];
                    if ([vc isKindOfClass:[PCScanViewController class]]) {
                        isClean = YES;
                    } else {
                        if (!isClean) {
                            [newviewControlles addObject:vc];
                        }
                    }
                }
                [newviewControlles addObject:viewController];
                [viewPage.navigationController setViewControllers:newviewControlles animated:YES];
            }
        }
        
        
    }else{
        
        
        if ([PCDUtil nilOrEmptyString:strFrameworkType]) {
            strFrameworkType = @"6";
        }
        if ([PCDUtil nilOrEmptyString:appName]) {
            appName =[PCDUtil channelId];
        }
        
        NSDictionary *params = @{@"bid":[PCDUtil safeString:appId],@"url":[PCDUtil safeString:strUrl],@"frameworkType":strFrameworkType,@"appId":[PCDUtil safeString:appName]};
        
        if (!PCDPageViewMangeGet().bOnLine) {
            
            [[PCDLoadPackageManage sharedService] loadSixPackageWithBid:params CompletionBlock:^(id  _Nonnull object, BOOL success, NSString * _Nonnull error) {
                //
                if (success) {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        NSDictionary *tempDic = (NSDictionary *)object;

                        [PCDPageViewMangeGet() creatCacheWebControllerWithView:nil Params:tempDic Status:NO];
                       
                    });
                }else{
                    [viewPage.view makeToast:error];
                }
            }];
            
        }else{
            
            NSString *strPageUrl = PCDPageViewMangeGet().baseUrl;
            
            if (![PCDUtil nilOrEmptyString: strUrl]) {
                strPageUrl = [NSString stringWithFormat:@"%@#%@/%@",strPageUrl,appId,strUrl];
//                strPageUrl = [NSString stringWithFormat:@"%@#%@",strPageUrl,strUrl];
            }
            NSDictionary *params = @{@"appId":appId,@"baseurl":PCDPageViewMangeGet().baseUrl,@"url":strPageUrl};

            [PCDPageViewMangeGet() creatCacheWebControllerWithView:nil Params:params Status:NO];
        }
        
    }



    //执行页面回调
    if (![PCDUtil nilOrEmptyString:callBack]) {
        evaluateJavaScriptOnMainThread(self,callBack,@"");
    }

}

-(NSInteger) getPageIndex:(NSString *)appId
{
    PCDPageViewMangeGet().index ++;
    
    return PCDPageViewMangeGet().index;
}

@end
